package com.selfdriving.activity;

import com.selfdriving.R;
import com.selfdriving.dialog.FlippingLoadingDialog;
import com.selfdriving.utils.DeviceUtils;
import com.selfdriving.utils.PhotoUtil;
import com.selfdriving.view.animation.*;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.widget.Button;
import android.widget.ImageView;

public class FeatureRecordActivity extends BaseActivity {
	@ViewInject(id = R.id.ivIcon)
	ImageView ivIcon;

	@ViewInject(id = R.id.btnChange, click = "btnChange")
	Button btnChange;

	@ViewInject(id = R.id.btnAdd, click = "btnAdd")
	Button btnAdd;

	private FlippingLoadingDialog dialog;

	@ViewInject(id = R.id.relativelayout_shrink, click = "shrinkRelativeLayout")
	View shrinkRelativeLayout;

	@ViewInject(id = R.id.imageview_plus)
	ImageView imageViewPlus;
	
	@ViewInject(id=R.id.menus_wrapper)
	ViewGroup menusWrapper;

	// 顺时针旋转动画
	private Animation animRotateClockwise;

	// 逆时针旋转动画
	private Animation animRotateAntiClockwise;
	
	private boolean	areMenusShowing;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feature_record);
		dialog = new FlippingLoadingDialog(this, "皮肤下载中 请稍后");
		animRotateClockwise = AnimationUtils.loadAnimation(this,
				R.anim.rotate_clockwise);
		animRotateAntiClockwise = AnimationUtils.loadAnimation(this,
				R.anim.rotate_anticlockwise);
		for (int position = 0; position < menusWrapper.getChildCount(); position++) 
		{
			menusWrapper.getChildAt(position).setOnClickListener(
					new SpringMenuLauncher(null, position));
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	public void btnChange(View v) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				PhotoUtil
						.getDrawable("http://imgsrc.baidu.com/forum/w%3D580%3Bcp%3Dtieba%2C10%2C477%3Bap%3D%CE%E2%BA%E8%E7%F9%B0%C9%2C90%2C485/sign=e1efef19b1de9c82a665f9875cbae37a/cc080ff0f736afc30a863445b119ebc4b64512cb.jpg");
			}
		}).start();
	}

	public void btnAdd(View v) {
		if (!isLogin()) {
			Intent intent = new Intent(this, RegisterActivity.class);
			startActivity(intent);
		} else {
			Intent intent = new Intent(this, AddFeatureRecordActivity.class);
			startActivity(intent);
		}
	}

	public void shrinkRelativeLayout(View v) {
		int[] size = DeviceUtils.getScreenSize(this);

		if (!areMenusShowing) {
			SpringAnimation.startAnimations(this.menusWrapper,
					ZoomAnimation.Direction.SHOW, size);
			this.imageViewPlus.startAnimation(this.animRotateClockwise);
		} else {
			SpringAnimation.startAnimations(this.menusWrapper,
					ZoomAnimation.Direction.HIDE, size);
			this.imageViewPlus.startAnimation(this.animRotateAntiClockwise);
		}

		areMenusShowing = !areMenusShowing;
	}
	
	// 分布菜单事件监听器
		private class SpringMenuLauncher implements OnClickListener {
			//private final Class<?> cls;
			private int position;

			private SpringMenuLauncher(Class<?> c,int position) {
				//this.cls = c;
				this.position = position;
			}

			public void onClick(View v) {
				FeatureRecordActivity.this.startSpringMenuAnimations(v);
			}
		}
		
		/**
		 * 展现菜单动画效果
		 * @param view
		 * @param runnable
		 */
		private void startSpringMenuAnimations(View view) {
			areMenusShowing = true;
			Animation shrinkOut1 = new ShrinkAnimationOut(300);
			Animation growOut = new EnlargeAnimationOut(300);
			shrinkOut1.setInterpolator(new AnticipateInterpolator(2.0F));
			shrinkOut1.setAnimationListener(new Animation.AnimationListener() {

				public void onAnimationEnd(Animation animation) {
					FeatureRecordActivity.this.imageViewPlus.clearAnimation();
				}

				public void onAnimationRepeat(Animation animation) {
					
				}

				public void onAnimationStart(Animation animation) {
					
				}
			});
			
			view.startAnimation(growOut);
		}

}
